package com.hiwan.unions.util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * @ClassName: AES
 * @Description: AES加密解密工具类
 * @Author yangsl
 * @Date 2016年2月23日 上午10:06:54
 * @Modify
 * @CopyRight hipay
 */

public class AESKit {

    private static final String ALGORITHM = "AES";

    /**
     * base64编码后进行加密在进行编码
     *
     * @param data   data 要加密的文本
     * @param rawKey rawKey 必须为16位字符
     * @return String
     * @see [类、类#方法、类#成员]
     */
    public static String encryptAndBase64(String data, String rawKey) {
        // rawKey = "1111111111111111111111111111111111111111111111111111111111";
        byte[] key = rawKey.getBytes();
        // Instantiate the cipher
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(Base64Util.encode(data.getBytes()).getBytes());
            return Base64Util.encode(encrypted);
        } catch (Exception e) {
//            Logger.e(e.getMessage() + e);
            return "";
        }
    }

    /**
     * 加密后进行base64编码
     *
     * @param data
     * @param rawKey
     * @return
     */
    public static String encryptAndBase64(byte[] data, byte[] rawKey) {
        byte[] key = rawKey;
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

            byte[] encrypted = cipher.doFinal(data);
            return Base64Util.encode(encrypted);
        } catch (Exception e) {
//            Logger.e(e.getMessage() + e);
            return "";
        }
    }

    /**
     * base64解码后进行解密
     *
     * @param encrypted 要解密的字符 encrypted
     * @param rawKey    rawKey 必须为16位且是加密时用的密钥
     * @return String
     * @see [类、类#方法、类#成员]
     */
    public static String decodebase64AndDecrypt(String encrypted, String rawKey) {
        try {
            if (encrypted.equals("")) {
                return "";
            }
            byte[] tmp = Base64Util.decode(encrypted);
            byte[] key = rawKey.getBytes();
            return Base64Util.decodeStrNoTry(decrypt(tmp, key));
        } catch (Exception e) {
//            Logger.e(e.getMessage() + e);
            return "";
        }
    }

    /**
     * base64解码后进行解密
     *
     * @param encrypted
     * @param rawKey
     * @return
     */
    public static String decodebase64AndDecrypt(byte[] encrypted, byte[] rawKey) {
        try {
            byte[] tmp = Base64Util.decode(encrypted);
            byte[] key = rawKey;
            return decrypt(tmp, key);
        } catch (Exception e) {
//            Logger.e(e.getMessage() + e);
            return "";
        }
    }

    /**
     * 直接解密
     *
     * @param encrypted
     * @param rawKey
     * @return
     */
    public static String decrypt(byte[] encrypted, byte[] rawKey) {
        try {
            byte[] tmp = encrypted;
            byte[] key = rawKey;

            SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);

            byte[] decrypted = cipher.doFinal(tmp);
            return new String(decrypted);
        } catch (Exception e) {
//            Logger.e(e.getMessage() + e);
            return "";
        }
    }

    public static byte[] decryptToByte(byte[] encrypted, byte[] rawKey) {
        try {
            byte[] tmp = encrypted;
            byte[] key = rawKey;

            SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);

            byte[] decrypted = cipher.doFinal(tmp);
            return decrypted;
        } catch (Exception e) {
//            Logger.e(e.getMessage() + e);
            return new byte[]{};
        }
    }
}
